
keys=['a','b','c']
values=[1,2,3]
print("使用字典推导式"
      "字典推导式是创建字典的优雅方式，特别适合从列表或其他字典中快速构建新字典")
my_dict={k:v for k,v in zip(keys,values)}
my_dict_2=dict(zip(keys,values))
print(my_dict)
print(my_dict_2)
data=[('Alice',25),('Bob',30)]
my_data={name:age for name,age in data}
print(data)
print(my_data)




print("get()方法避免KeyError")
my_dict={'name':'Alice'}
print(my_dict.get("age","default value"))
# print(my_dict["age"])

print("利用setdefault()添加键值对 添加字典值并获取添加的值")
my_dict={'name':'Alice'}
res = my_dict.setdefault("age",25)
res2=my_dict["sex"]=1
print(res)
print(res2)

print("合并字典")
my_dict={'name':'Alice'}
new_info={'city':'Wonderland'}
res={**my_dict,**new_info}
print(res)
my_dict.update(new_info)
print(my_dict)
merge_dict=my_dict | new_info
print(merge_dict)

print("字典视图对象  .keys(), .values(), 和 .items()返回的是视图对象，它们在字典改变时动态更新")
my_dict={'name':'Alice'}
print(my_dict.keys())
print(list(my_dict.keys()))
print(type(my_dict.keys()))
print(my_dict.values())
print(list(my_dict.values()))
print(type(my_dict.values()))
print(my_dict.items())
print(list(my_dict.items()))
print(list(my_dict.items())[0])
print(type(my_dict.items()))

print("判断数据类型")
my_dict={'name':'Alice'}
print(isinstance(my_dict,dict))
print(isinstance(my_dict,list))
print(isinstance(my_dict,tuple))

print("字典递归操作")
def process_dict(d:dict):
    for k,v in d.items():
        if isinstance(v,dict):
            process_dict(v)
        else:
            print(f"Key:{k},Value:{v}")
nestec_dict={'data':{'info':{'name':'Alice'}}}
process_dict(nestec_dict)
print("数据分析")
scores={'Alice':85,'Bob':90,'Charlie':78}
print("平均值")
average=sum(scores.values())/len(scores)
print(average)
print("最高分")
max_score_student=max(scores,key=scores.get)
print(max_score_student)
print("最低分")
min_score_student=min(scores,key=scores.get)
print(min_score_student)
min_score_student_2=min(scores,key=lambda k:scores[k])
print(min_score_student_2)
from operator import itemgetter
min_score_student_3=min(scores,key=itemgetter(1))
print(min_score_student_3)

print("反转单层字典")
# my_dict={'name': 'Alice', 'city': 'Wonderland'}
my_dict={'name': 'Alice', 'city': 'Alice'}
revers_my_dict={v:k for k,v in my_dict.items()}
print(revers_my_dict)
def process_dict(d:dict):
    for k,v in d.items():
        if isinstance(v,dict):
            process_dict(v)
        else:
            print(f"Key:{k},Value:{v}")

names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
res=list(zip(names, ages))
print(res)

strings = ["hello", "world"]
res=list(map(str.upper, strings))
print(res)

print("扁平化嵌套列表")
nested_list = [[1, 2], [3, 4], [5, 6]]
res=[item for sub_list in nested_list for item in sub_list]
print(res)

print("列表中查找出现频率最高的元素")
num_list=[1, 2, 2, 3, 3, 3, 4, 4]
res=max(num_list,key=num_list.count)
print(res)



# import collections
# res=dict(collections.Counter(num_list))
# print(res)
# from operator import itemgetter
# res = {1: 1, 2: 2, 3: 3, 4: 2}
# res_key=max(res,key=lambda item:item[1])
# print(res_key)
# # min_score_student_3=min(scores,key=itemgetter(1))


res = {1: 1, 2: 2, 3: 3, 4: 2}
# 使用 lambda 函数来获取字典项的值，并返回键
res_key = max(res.items(), key=lambda item: item[1])
print(res_key)